package im.yisme.view.activities;

import java.util.ArrayList;

import im.yisme.R;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import im.yisme.view.ui.MainView;
import im.yisme.view.ui.YView;
import im.yisme.view.ui.animation.BGP;
import im.yisme.view.ui.component.OnClickListener;
import im.yisme.view.ui.component.YButton;
import im.yisme.view.ui.util.ButtonEventListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;

public class MainActivity extends Rotate3DActivity {
	
	private ViewService viewCtrl;
	private MainView mainView;
	private ViewGroup playListView,recordListView,musicLibView,lyricsLibView;
	private ButtonEventListener eventListener;
	private YButton toLyrics, toMusic, toPlay, toRecord;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		viewCtrl = ViewServiceProvider.getViewService();
		
		init();
		setContentView(mainView);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		viewCtrl.updateTopActivity(this);
	}
	
	protected void onDestroy(){
		super.onDestroy();
		mainView.stop();
	}
	
	protected void onPause(){
		super.onPause();
		mainView.pause();
	}
	
	protected void onRestart(){
		super.onRestart();
		mainView.resume();
	}
	
	protected void onStop(){
		super.onStop();
		finish();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return eventListener.onButtonEvent(event);
	}
	
	private void init(){
		mainView = new MainView(this);
		initButtons();
		initButtonListeners();
		registerButtons();
	}
	
	private void registerButtons(){
		eventListener = new ButtonEventListener();
		eventListener.registerButton(toPlay);
		eventListener.registerButton(toRecord);
		eventListener.registerButton(toMusic);
		eventListener.registerButton(toLyrics);
	}
	
	private void initButtons(){
		Resources r = getResources();
		
		ArrayList<PointF> toPlayPointFs = new ArrayList<PointF>();
		toPlayPointFs.add(new PointF(r.getDimension(R.dimen.main_game_range_1_x), r.getDimension(R.dimen.main_game_range_1_y)));
		toPlayPointFs.add(new PointF(r.getDimension(R.dimen.main_game_range_2_x), r.getDimension(R.dimen.main_game_range_2_y)));
		toPlayPointFs.add(new PointF(r.getDimension(R.dimen.main_game_range_3_x), r.getDimension(R.dimen.main_game_range_3_y)));
		toPlayPointFs.add(new PointF(r.getDimension(R.dimen.main_game_range_4_x), r.getDimension(R.dimen.main_game_range_4_y)));
		toPlay = new YButton();
		toPlay.setPoints(toPlayPointFs);
		
		ArrayList<PointF> toRecordPointFs = new ArrayList<PointF>();
		toRecordPointFs.add(new PointF(r.getDimension(R.dimen.main_record_range_1_x), r.getDimension(R.dimen.main_record_range_1_y)));
		toRecordPointFs.add(new PointF(r.getDimension(R.dimen.main_record_range_2_x), r.getDimension(R.dimen.main_record_range_2_y)));
		toRecordPointFs.add(new PointF(r.getDimension(R.dimen.main_record_range_3_x), r.getDimension(R.dimen.main_record_range_3_y)));
		toRecordPointFs.add(new PointF(r.getDimension(R.dimen.main_record_range_4_x), r.getDimension(R.dimen.main_record_range_4_y)));
		toRecord = new YButton();
		toRecord.setPoints(toRecordPointFs);
		
		ArrayList<PointF> toMusicPointFs = new ArrayList<PointF>();
		toMusicPointFs.add(new PointF(r.getDimension(R.dimen.main_music_range_1_x), r.getDimension(R.dimen.main_music_range_1_y)));
		toMusicPointFs.add(new PointF(r.getDimension(R.dimen.main_music_range_2_x), r.getDimension(R.dimen.main_music_range_2_y)));
		toMusicPointFs.add(new PointF(r.getDimension(R.dimen.main_music_range_3_x), r.getDimension(R.dimen.main_music_range_3_y)));
		toMusicPointFs.add(new PointF(r.getDimension(R.dimen.main_music_range_4_x), r.getDimension(R.dimen.main_music_range_4_y)));
		toMusic = new YButton();
		toMusic.setPoints(toMusicPointFs);
		
		ArrayList<PointF> toLyricsPointFs = new ArrayList<PointF>();
		toLyricsPointFs.add(new PointF(r.getDimension(R.dimen.main_lyrics_range_1_x), r.getDimension(R.dimen.main_lyrics_range_1_y)));
		toLyricsPointFs.add(new PointF(r.getDimension(R.dimen.main_lyrics_range_2_x), r.getDimension(R.dimen.main_lyrics_range_2_y)));
		toLyricsPointFs.add(new PointF(r.getDimension(R.dimen.main_lyrics_range_3_x), r.getDimension(R.dimen.main_lyrics_range_3_y)));
		toLyricsPointFs.add(new PointF(r.getDimension(R.dimen.main_lyrics_range_4_x), r.getDimension(R.dimen.main_lyrics_range_4_y)));
		toLyrics = new YButton();
		toLyrics.setPoints(toLyricsPointFs);
	}
	
	private void initButtonListeners(){
		toPlay.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				mainView.stop();
				mainView.startAnimation(rOutAnimation);
				initPlayListView();
				playListView.startAnimation(lInAnimation);
				
				lInAnimation.setAnimationListener(new AnimationListener() {
					
					public void onAnimationStart(Animation animation) {}
					public void onAnimationRepeat(Animation animation) {}
					
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MainActivity.this, PlayMusicListActivity.class);
						viewCtrl.jumpTo(intent);
						overridePendingTransition(0, 0);
					}
				});
				
			}
		});
		
		toRecord.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				mainView.stop();
				mainView.startAnimation(lOutAnimation);
				initRecordListView();
				recordListView.startAnimation(rInAnimation);
				
				rInAnimation.setAnimationListener(new AnimationListener() {
					
					public void onAnimationStart(Animation animation) {}
					public void onAnimationRepeat(Animation animation) {}
					
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MainActivity.this, RecordMusicListActivity.class);
						viewCtrl.jumpTo(intent);
						overridePendingTransition(0, 0);
					}
				});
			}
		});
		
		toMusic.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				mainView.stop();
				mainView.startAnimation(rOutAnimation);
				initMusicLibView();
				musicLibView.startAnimation(lInAnimation);
				
				lInAnimation.setAnimationListener(new AnimationListener() {
					
					public void onAnimationStart(Animation animation) {}
					public void onAnimationRepeat(Animation animation) {}
					
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MainActivity.this, MusicLibraryActivity.class);
						viewCtrl.jumpTo(intent);
						overridePendingTransition(0, 0);
					}
				});
			}
		});
		
		toLyrics.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				mainView.stop();
				mainView.startAnimation(lOutAnimation);
				initLyricsLibView();
				lyricsLibView.startAnimation(rInAnimation);
				
				rInAnimation.setAnimationListener(new AnimationListener() {
					
					public void onAnimationStart(Animation animation) {}
					public void onAnimationRepeat(Animation animation) {}
					
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(MainActivity.this, LyricsLibraryActivity.class);
						viewCtrl.jumpTo(intent);
						overridePendingTransition(0, 0);
					}
				});
			}
		});
	}
	
	private void initPlayListView(){
		setContentView(R.layout.activity_musiclist);
		
		playListView = (RelativeLayout) findViewById(R.id.musiclist);
	}
	
	private void initRecordListView(){
		setContentView(R.layout.activity_musiclist);
		
		recordListView = (RelativeLayout) findViewById(R.id.musiclist);
		ImageView title = (ImageView) findViewById(R.id.imageView_title);
		title.setImageResource(R.drawable.record_music_list_title);
	}
	
	private void initMusicLibView(){
		setContentView(R.layout.activity_musiclib);
		
		musicLibView = (RelativeLayout) findViewById(R.id.musiclib);
	}
	
	private void initLyricsLibView(){
		setContentView(R.layout.activity_lyricslib);
		
		lyricsLibView = (RelativeLayout) findViewById(R.id.lyricslib);
	}
}
